#include "TFKLinePushHandler.h"
#include <tfbondlib/tds/TfKLineRealTime_1M.h>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>

TFKLinePushHandler::TFKLinePushHandler()
{
	connect(&TfKLineRealTime_1M::instance(), &TfKLineRealTime_1M::kLinePushDataArrived, this, &TFKLinePushHandler::onKLinePushDataArrived);
}

TFKLinePushHandler::~TFKLinePushHandler()
{
    for (auto& r : m_registers) {
        TfKLineRealTime_1M::instance().unRegisterPush(r.first);
    }
}

bool TFKLinePushHandler::processCmd(const QJsonObject& cmdObj, const ReqInfo& reqinfo, bool unReg /*= false*/)
{
	QJsonObject condObj = cmdObj["cond"].toObject();
	if (condObj.isEmpty() && unReg)
	{
		for (auto& r : m_registers)
		{
			for (auto iter = r.second.begin(); iter != r.second.end();)
			{
				if (iter->m_webHandler != reqinfo.m_webHandler)
				{
					++iter;
					continue;
				}

				iter = r.second.erase(iter);
				if(r.second.empty())
					TfKLineRealTime_1M::instance().unRegisterPush(r.first);
			}

			removePush(r.first, reqinfo);
		}
	}
	if (condObj.isEmpty())
		return false;

	std::string id = condObj["id"].toString().toUtf8().data();
	std::string key = getTfKey(id.c_str());
	if (key.empty())
		return false;

	if (!unReg)
	{
		addPush(id, reqinfo);
		TfKLineRealTime_1M::instance().registerPush(id);
		m_registers[id].push_back(reqinfo);
	}
	else
	{
		removePush(id, reqinfo);
		for (auto iter = m_registers[id].begin(); iter != m_registers[id].end();)
		{
			if (iter->m_webHandler == reqinfo.m_webHandler)
				iter = m_registers[id].erase(iter);
			else
				++iter;
		}
		if (m_registers[id].empty())
			TfKLineRealTime_1M::instance().unRegisterPush(id);
	}

	return true;
}

void TFKLinePushHandler::onKLinePushDataArrived(QString id, int beginIndex, int endIndex)
{
	std::string key = getTfKey(id.toUtf8().data());
	if (key.empty() || m_registers.find(id.toUtf8().data()) == m_registers.end())
		return;

	std::vector<QString> res;
	std::vector<xQBTFRealTimeUnit_c> data;
	if (!TfKLineRealTime_1M::instance().getRealtime(id.toUtf8().data(), data, beginIndex, endIndex))
		return;

	QJsonArray jsList;
	QJsonObject jsRes;
	int count = 0;
	for (const auto& unit : data)
	{
		QJsonObject item;
		item["id"] = unit.m_TF_ID;
		item["key"] = unit.m_TF_Key;
		item["time"] = (int)unit.m_Tran_Time;
		item["open"] = unit.m_Open;
		item["high"] = unit.m_High;
		item["low"] = unit.m_Low;
		item["close"] = unit.m_Close;
		item["position"] = unit.m_Positions;
		item["vol"] = unit.m_Volume;
		item["totalvol"] = unit.m_Volume_Total;
		item["totalamount"] = unit.m_Amount_Total;
		jsList.append(item);

		if (++count % 20 == 0)
		{
			jsRes["finish"] = 0;
			jsRes["list"] = jsList;

			QJsonDocument doc(jsRes);
			pushData(id.toUtf8().data(), doc.toJson(QJsonDocument::Compact));

			QJsonObject emptyObj;
			QJsonArray emptyArr;
			jsRes.swap(emptyObj);
			jsList.swap(emptyArr);
		}
	}
	jsRes["finish"] = 1;
	jsRes["list"] = jsList;

	QJsonDocument doc(jsRes);
	pushData(id.toUtf8().data(), doc.toJson(QJsonDocument::Compact));
}
